package com.kedacom.freedomLabel.utils;

import android.os.Environment;

import java.io.File;
import java.math.BigDecimal;

/**
 * 文本读写辅助类
 */
public class FileHelp {

    public static final String pathNormal = "/FreedomLabel/";

    /**
     * 本应用的外部数据库
     */
    public static final String DATABASE_DIR = Environment.getExternalStorageDirectory() +
            pathNormal + "database/";
    /**
     * 用于储存静默拍照的图片
     */
    public static final String DATA_PIC_DIR = Environment.getExternalStorageDirectory() +
            pathNormal + "/picture/";

    /**
     * excle导出路径
     */
    public static final String EXCEL_DIR = Environment.getExternalStorageDirectory() +
            pathNormal + "/excel/";

    /**
     * 自动生成报表目录
     */
    public static final String AUTO_EXCEL_DIR = EXCEL_DIR + "/AutoExportReport/";

    /**
     * 临时报表目录
     */
    public static final String TEMP_EXCEL_DIR = EXCEL_DIR + "/TempExportReport/";

    public static final String APP_ROOT_DIR = Environment.getExternalStorageDirectory() + pathNormal;

    /**
     * 本地设备基本信息（设备号）
     */
    private static final String DEVICE_PATH = "/data/seray/config/freedom_device.cfg";
    /**
     * 本地屏幕校准文件
     */
//    private static final String CALIBRATION_PATH = "/data/system/freedom_calibration";
    private static final String CALIBRATION_PATH = "/data/system/calibration";

    /**
     * 删除本地屏幕校准文件
     */
    public static boolean deleteCalibrationFile() {
        File file = new File(CALIBRATION_PATH);
        boolean isExists = file.exists();
        boolean isDeLete = file.delete();
        if (file.exists()) {
            file.delete();
        }
        return true;

    }

    /**
     * 删除指定路径下的文件夹
     */
    public static boolean deleteDir(String dirPath) {
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            return false;
        }
        if (dirFile.isFile()) {
            return dirFile.delete();
        } else {
            File[] files = dirFile.listFiles();
            for (File file : files) {
                String root = file.getAbsolutePath();
                deleteDir(root);
            }
            return dirFile.delete();
        }
    }

    /**
     * 获取当前SDCard可用内存大小
     */
    public static BigDecimal getSDCardUsableSpace() {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            return BigDecimal.ZERO;
        }
        File storageDirectory = Environment.getExternalStorageDirectory();
        long usableSpace = storageDirectory.getUsableSpace();
        long formatSpace = usableSpace / 1024 / 1024;//change to MB
        return new BigDecimal(Long.toString(formatSpace)).setScale(2, BigDecimal.ROUND_HALF_UP);
    }
}
